package com.zhongxing.hdy.spritel_hdy.view;

import android.content.Context;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.zhongxing.hdy.spritel_hdy.R;
import com.zhongxing.hdy.spritel_hdy.util.AnimUtil;

/**
 * Created by Administrator on 2017/7/1.
 */

public class ShopView extends BaseView{
    private ImageView iv_chongzhi;
    private ImageView iv_fugure;  //美女
    private FrameLayout fl_btns; //商店
    private FrameLayout fl_frame; //底部广告

    private AnimationSet animationSetFugure;
    //充值界面
    private ChongZhiView chongZhiView;

    public ShopView(Context context){
        super(context);
        setContentView(R.layout.view_shop);

        initUI();
        fugureAnimation();
    }

    private void fugureAnimation(){
        //?????????????????
        animationSetFugure=new AnimationSet(false);
        //美女无限重复动
        ScaleAnimation scaleAnimation = new ScaleAnimation(1f, 1f, 0.9f, 0.8f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 1f);
        scaleAnimation.setDuration(800);
        scaleAnimation.setRepeatCount(Animation.INFINITE);//无限重复
        scaleAnimation.setRepeatMode(Animation.REVERSE);
        //iv_fugure.startAnimation(scaleAnimation);
		animationSetFugure.addAnimation(scaleAnimation);

        //位移 美女 从左到右 进场动画
        TranslateAnimation transX=new TranslateAnimation(Animation.RELATIVE_TO_SELF, -1f, Animation.RELATIVE_TO_SELF, 0f,
                Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f);
        transX.setDuration(300);
        animationSetFugure.addAnimation(transX);

    }

    private TranslateAnimation addAnimation(View view, float fromXValue, float toXValue, float fromYValue, float toYValue, long animatTime){
        TranslateAnimation transY=new TranslateAnimation(Animation.RELATIVE_TO_SELF,fromXValue,Animation.RELATIVE_TO_SELF,toXValue,
                Animation.RELATIVE_TO_SELF,fromYValue,Animation.RELATIVE_TO_SELF,toYValue);
        transY.setDuration(animatTime);
        view.startAnimation(transY);
        //设置返回值是为了监听
        return transY;
    }

    public void show() {
//        TranslateAnimation transY=new TranslateAnimation(Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0f,
//                Animation.RELATIVE_TO_SELF,-1f,Animation.RELATIVE_TO_SELF,0f);
//        transY.setDuration(300);
//        fl_btns.startAnimation(transY);
        TranslateAnimation transY =addAnimation(fl_btns,0f,0f,-1f,0f,300); //商店下落
        addAnimation(fl_frame,0f,0f,1f,0f,300); //广告栏上升

        transY.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                //iv_fugure.setVisibility(View.VISIBLE);
                iv_fugure.startAnimation(animationSetFugure);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }

    public void hide(Animation.AnimationListener animationListener) {
//        TranslateAnimation transY = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f,
//                Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, -1f);
//        transY.setDuration(300);
//        fl_btns.startAnimation(transY);

        TranslateAnimation transY =addAnimation(fl_btns,0f,0f,0f,-1f,300); //商店上升
        addAnimation(fl_frame,0f,0f,0f,1f,300); //广告栏下落

//        TranslateAnimation transX = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, -1f,
//                Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f);
//        transX.setDuration(300);
//        iv_fugure.startAnimation(transX);
        TranslateAnimation transX = addAnimation(iv_fugure,0f,-1f,0f,0f,300); //美女左退
        if (animationListener != null) {
            transX.setAnimationListener(animationListener);

        }
    }

    @Override
    protected void initUI(){
        chongZhiView=new ChongZhiView(context);
        iv_chongzhi = (ImageView) findViewById(R.id.iv_chongzhi);
        AnimUtil.addOnTouchListener(iv_chongzhi, 0.5f, 0.5f, new AnimUtil.AnimListener() {
            @Override
            public void down(View view) {

            }

            @Override
            public void move(View view) {

            }

            @Override
            public void up(View view) {
                chongZhiView.show();
            }
        });
        iv_fugure = (ImageView) findViewById(R.id.iv_figure);
        iv_fugure.setVisibility(View.INVISIBLE);
        fl_btns = (FrameLayout) findViewById(R.id.fl_btns);
        fl_frame = (FrameLayout) findViewById(R.id.fl_frame);
    }
}
